package com.biz.primus.ms.commodity.service.backend;


import com.biz.primus.base.enums.commodity.IStatus;
import com.biz.primus.base.vo.KeywordWeightVo;
import com.biz.primus.ms.base.service.AbstractBaseService;
import com.biz.primus.ms.commodity.dao.po.KeywordWeightConfig;
import com.biz.primus.ms.commodity.dao.repository.keywordConfig.KeywordConfigRepository;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Nullable;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class KeywordWeightService extends AbstractBaseService {

    @Autowired
    private KeywordConfigRepository keywordConfigRepository;

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    @Transactional
    public KeywordWeightVo saveOrUpdate(KeywordWeightVo keywordWeightVo) {
        if (keywordWeightVo.getStatus() == IStatus.DELETE){
            List<KeywordWeightConfig> normalLists = keywordConfigRepository.findByDeleteFlagAndStatusOrderByUpdateTimestampDesc(Boolean.FALSE, IStatus.NORMAL);
            if (CollectionUtils.isNotEmpty(normalLists)){
                if (normalLists.size() == 1 && StringUtils.isNotBlank(keywordWeightVo.getId()) &&
                        Objects.equals(String.valueOf(normalLists.get(0).getId()),keywordWeightVo.getId())){
                    throw new IllegalArgumentException("权重配置列表中至少应该有一个启用的配置");
                }
            }else {
                throw new IllegalArgumentException("权重配置列表中至少应该有一个启用的配置");
            }
        }
        log.info("KeywordWeightServiceImpl saveOrUpdate reqVo[{}]",keywordWeightVo);
        KeywordWeightConfig keywordWeightConfig = new KeywordWeightConfig();
        if (StringUtils.isBlank(keywordWeightVo.getId())){
            log.info("create new keywordWeightConfig");
            keywordWeightVo.setId(String.valueOf(idService.getNextId()));
            keywordWeightConfig.setId(Long.valueOf(keywordWeightVo.getId()));
        }else {
            keywordWeightConfig = keywordConfigRepository.findOne(Long.valueOf(keywordWeightVo.getId()));
        }
        BeanUtils.copyProperties(keywordWeightVo,keywordWeightConfig);
        keywordConfigRepository.save(keywordWeightConfig);
        return keywordWeightVo;
    }

    public List<KeywordWeightVo> listAll() {
        List<KeywordWeightConfig> lists = keywordConfigRepository.findByDeleteFlagOrderByUpdateTimestampDesc(Boolean.FALSE);
        return Lists.transform(lists, new Function<KeywordWeightConfig, KeywordWeightVo>() {
            @Nullable
            @Override
            public KeywordWeightVo apply(@Nullable KeywordWeightConfig input) {
                KeywordWeightVo keywordWeightVo = new KeywordWeightVo();
                BeanUtils.copyProperties(input,keywordWeightVo);
                if (input.getUpdateTimestamp() == null){
                    keywordWeightVo.setUpdateTime("无");
                }else {
                    keywordWeightVo.setUpdateTime(sdf.format(input.getUpdateTimestamp()));
                }
                keywordWeightVo.setId(String.valueOf(input.getId()));
                return keywordWeightVo;
            }
        });
    }

    public KeywordWeightVo getConfig() {
        List<KeywordWeightConfig> configList = keywordConfigRepository.findByDeleteFlagAndStatusOrderByUpdateTimestampDesc(Boolean.FALSE, IStatus.NORMAL);
        KeywordWeightConfig keywordWeightConfig = configList.get(0);
        if (keywordWeightConfig == null){
            return null;
        }
        KeywordWeightVo resp = new KeywordWeightVo();
        BeanUtils.copyProperties(keywordWeightConfig,resp);
        return resp;
    }

    public KeywordWeightVo findOne(Long id) {
        KeywordWeightConfig keywordWeightConfig = keywordConfigRepository.findOne(id);
        KeywordWeightVo resp = new KeywordWeightVo();
        BeanUtils.copyProperties(keywordWeightConfig,resp);
        resp.setId(String.valueOf(keywordWeightConfig.getId()));
        return resp;
    }
}
